package cn.nubia.admin.application.service;

import cn.nubia.admin.common.mybatis.bean.Paged;
import cn.nubia.admin.common.mybatis.bean.Query;
import cn.nubia.admin.common.mybatis.service.BaseServiceImpl;
import cn.nubia.admin.common.springmvc.WebContext;
import cn.nubia.admin.common.utils.DateUtil;
import cn.nubia.admin.application.bean.NotifyPush;
import cn.nubia.admin.application.query.NotifyPushQuery;
import cn.nubia.admin.sysconfig.bean.SystemLog;
import cn.nubia.admin.sysconfig.service.SystemLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by justin on 15/7/15.
 */
@Service
public class NotifyPushService extends BaseServiceImpl<NotifyPush> {
    @Autowired
    SystemLogService systemLogService;


    public Paged<NotifyPush> listNotifyPushes(NotifyPushQuery notifyPushQuery) {
        Query query = Query.build(NotifyPush.class);
        if(notifyPushQuery.getMsgObj()!=null && notifyPushQuery.getMsgObj()>=0){
            query.addEq(NotifyPush.COLUMN_MSGOBJ,notifyPushQuery.getMsgObj());
        }
        if(notifyPushQuery.getBeginDateRange()!=null && !"".equals(notifyPushQuery.getBeginDateRange())){
            query.addBetween(NotifyPush.COLUMN_BEGINTIME, DateUtil.getIntegerTime(DateUtil.getBeginDateFromRange(notifyPushQuery.getBeginDateRange()))-1,
                    DateUtil.getIntegerTime(DateUtil.getEndDateFromRage(notifyPushQuery.getBeginDateRange())));
        }
        query.addNotEq(NotifyPush.COLUMN_STATE,NotifyPush.STATE_DEL);
        query.addOrder(NotifyPush.COLUMN_BEGINTIME, Query.DBOrder.DESC);
        query.setPaged(notifyPushQuery.getPageNo(),notifyPushQuery.getPageSize());
        Paged<NotifyPush> notifyPushPaged = findPagedByQuery(query);
        for(NotifyPush notifyPush:notifyPushPaged.getListData()){
            notifyPush.setMsgObjShow(NotifyPush.msgObjMap.get(String.valueOf(notifyPush.getMsgObj())));
        }
        return notifyPushPaged;
    }

    public void saveNotify(NotifyPush notifyPush) {
        Integer curr = DateUtil.getCurrentTime();
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        if(NotifyPush.MSGOBJ_APP==notifyPush.getMsgObj()){
            notifyPush.setResId(notifyPush.getSoftradio());
        }else if(NotifyPush.MSGOBJ_CATETORY == notifyPush.getMsgObj()){
            notifyPush.setResId(notifyPush.getCategoryradio());
        }else if(NotifyPush.MSGOBJ_TOPIC == notifyPush.getMsgObj()){
            notifyPush.setResId(notifyPush.getTopicradio());
        }
        notifyPush.setLastModifyTime(curr);
        notifyPush.setState(NotifyPush.STATE_NORMAL);
        if(notifyPush.getId()!=null&&notifyPush.getId()>0){
            NotifyPush notifyPushDomain = get(notifyPush.getId());
            notifyPush.setCreateTime(notifyPushDomain.getCreateTime());
            notifyPush.setCreateAdminId(notifyPushDomain.getCreateAdminId());
            notifyPush.setCreateAdminName(notifyPushDomain.getCreateAdminName());
            update(notifyPush);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.NOTIFYPUSHLIST, SystemLog.OperateTypeEnum.EDIT, notifyPush.getId());
        }else{
            notifyPush.setCreateTime(curr);
            notifyPush.setCreateAdminId(adminId);
            notifyPush.setCreateAdminName(adminName);
            insert(notifyPush);
            //log
            systemLogService.log(SystemLog.ModuleTypeEnum.NOTIFYPUSHLIST, SystemLog.OperateTypeEnum.ADD, notifyPush.getId());
        }
    }

    public NotifyPush getNotify(Integer id) {
        NotifyPush notifyPush = get(id);
        notifyPush.setBeginStr(DateUtil.getFormatDate(notifyPush.getBeginTime()));
        notifyPush.setEndStr(DateUtil.getFormatDate(notifyPush.getEndTime()));
        notifyPush.setCreateTimeShow(DateUtil.getFormatDate(notifyPush.getCreateTime()));
        notifyPush.setLastModifyTimeShow(DateUtil.getFormatDate(notifyPush.getLastModifyTime()));
        //log
        systemLogService.log(SystemLog.ModuleTypeEnum.NOTIFYPUSHLIST, SystemLog.OperateTypeEnum.DEL, notifyPush.getId());
        return notifyPush;
    }

    public void deleteNotify(Integer id) {
        NotifyPush notifyPush = get(id);
        notifyPush.setState(NotifyPush.STATE_DEL);
        Integer adminId = WebContext.getCachedUser().getId();//WebContext.getCachedUser().getId()
        String  adminName = WebContext.getCachedUser().getName();//WebContext.getCachedUser().getName()
        Integer curr = DateUtil.getCurrentTime();
        notifyPush.setLastModifyTime(curr);
        update(notifyPush);
    }
}
